function RegisterNull() {
    let form = document.formRegister;
    let pwd = form.password.value;
    let repwd = form.repassword.value;
    let phone = form.phone.value;
    let check = form.check.value;
    let perm = form.permission.value;
    if (pwd == "") {
        alert("请输入密码！");
        form.upass.focus();
        return false;
    }
    if (repwd == "") {
        alert("请输入确认密码！");
        form.reupass.focus();
        return false;
    }
    if (pwd != repwd) {
        alert("2次密码不一致，请重新输入！");
        form.upass.value = "";
        form.reupass.value = "";
        form.upass.focus();
        return false;
    }
    if (phone == "") {
        alert("请输入电话！");
        form.reupass.focus();
        return false;
    }
    if (check == "") {
        alert("请输入验证码！");
        form.reupass.focus();
        return false;
    }
    if (perm.length < 1) {
        alert("请选择角色！");
        return false;
    }
    // form.submit();
    $.post({
        url: "/bookcity/user/register",
        data: {
            "pwd": pwd,
            "phone": phone,
            "check": check,
            "perm": perm
        },
        success: function (data) {
            data = JSON.parse(data);
            if (data.isRegister) {
                alert("你的账号为：" + data.uid + "，请牢记！");
                wrapLogin();
                document.formLogin.account.value = data.uid;
            } else {
                alert("注册失败");
            }
        }
    });
    return true;
}

function LoginNull() {
    let form = document.formLogin;
    let acc = form.account.value;
    let pwd = form.password.value;
    let isnum = /^\d+$/;
    if (isnum.test(acc) == false) {
        alert("账号为全数字！");
        return false;
    }
    if (acc == "") {
        alert("请输入账号！");
        form.uname.focus();
        return false;
    }
    if (pwd == "") {
        alert("请输入密码！");
        form.upass.focus();
        return false;
    }
    // form.submit();
    $.post({
        url: "/bookcity/user/login",
        data: {
            "account": acc,
            "password": pwd
        },
        success: function (data) {
            data = JSON.parse(data);
            if (data.isRegister) {
                if (data.checkLogin) {
                    $("#successLogin > a > div").text(data.name);
                    closelogin();
                    alert("登录成功！");
                    successLogin();
                    $.cookie("uid", data.uid, { expires: 7, path: "/" });
                    $.cookie("uname", data.name, { expires: 7, path: "/" });
                    $.cookie("isUser", data.isUser, { expires: 7, path: "/" });
                    if (data.isUser) $("#successLogin > a").attr("href", "/bookcity/user/info/");
                    else $("#successLogin > a").attr("href", "/bookcity/user/onbook/");
                } else {
                    alert("账号或密码错误！");
                }
            } else {
                alert("账号不存在！");
            }
        }
    });
    return true;
}

/*点击弹出按钮*/
function openlogin() {
    document.getElementById('modal1').style.display = 'block';
}

/*超链接关闭登陆弹窗*/
function closelogin() {
    document.getElementById('modal1').style.display = 'none';
}

/*跳转到注册*/
function wrapRegister() {
    document.getElementById('login').style.display = 'none';
    document.getElementById('register').style.display = 'block';
}

/*跳转到登录*/
function wrapLogin() {
    document.getElementById('login').style.display = 'block';
    document.getElementById('register').style.display = 'none';
}

/*登录成功*/
function successLogin() {
    document.getElementById('notLogin').style.display = 'none';
    document.getElementById('successLogin').style.display = 'block';
}

/*分类中选取li元素*/

window.onload = () => {
    // 载入数据
    list_book(0, 30, 0, 0, 0);

    if ($.cookie("uid") != null) {
        successLogin();
        $("#successLogin > a > div").text($.cookie("uname"));
        if ($.cookie("isUser")) $("#successLogin > a").attr("href", "/bookcity/user/info/");
        else $("#successLogin > a").attr("href", "/bookcity/user/onbook/");
    }

    var page = 0, per_page = 30, type = 0, zone = 0, other = 0;

    $(".type li").each(function (i) {
        $(this).on("click", function () {
            clear_selected($(".type li"));
            $(this).addClass("selected");
            type = $(this).val();
            page = 0;
            list_book(page, per_page, type, zone, other);
        })
    })

    $(".zoon li").each(function (i) {
        $(this).on("click", function () {
            clear_selected($(".zoon li"));
            $(this).addClass("selected");
            zone = $(this).val();
            page = 0;
            list_book(page, per_page, type, zone, other);
        })
    })

    $(".other li").each(function (i) {
        $(this).on("click", function () {
            clear_selected($(".other li"));
            $(this).addClass("selected");
            other = $(this).val();
            page = 0;
            list_book(page, per_page, type, zone, other);
        })
    })

    $(".pageItems li").each(function (i) {
        $(this).on("click", function () {
            clear_selected($(".pageItems li"));
            $(this).addClass("selected");
            page = get_page_number(page, i);
            list_book(page, per_page, type, zone, other);
        })
    })

    /**
     * 将服务器返回的 string 型图书数据转换为 JSON 并写入页面
     * @param { String } data 
     */
    function write_book(data) {
        // !?
        data = JSON.parse(data);
        let bookname = $(".movieItem .eval a");
        let type = $(".movieItem div.dimax");
        let price = $(".movieItem .eval .value");
        let link = $(".movieItem>a");
        let items = $(".movieItem");
        // 写之前将旧数据复原
        for (let i = 0; i < items.length; i++) {
            items[i].hidden = false;
            type[i].hidden = false;
            type[i].innerText = "折扣";
        }
        let len = function (dlen, ilen) {
            if (dlen < ilen) {
                for (let i = dlen; i < ilen; i++) {
                    items[i].hidden = true;
                }
                return dlen;
            }
            else
                return ilen;
        }(data.count, items.length);

        data = data.data;
        // 写数据
        for (let i = 0; i < len; i++) {
            bookname[i].text = data[i].bname;
            price[i].innerText = data[i].price;
            link[i].href = "/bookcity/book/" + data[i].bid;
            if (data[i].btype == 0)
                type[i].hidden = true;
            else if (data[i].btype == 1)
                ;
            else if (data[i].btype == 2) {
                type[i].hidden = false;
                type[i].innerText = "免费";
                price[i].hidden = true;
            }
            else if (data[i].btype == 4) {
                type[i].hidden = false;
                type[i].innerText = "VIP";
            }
        }
    }

    /**
     * 请求图书数据并写入页面
     * @param {Number} page 页码
     * @param {Number} per_page 每页的数量
     * @param {Number} type 类型
     * @param {Number} zone 地区
     * @param {Number} other 其他
     */
    function list_book(page, per_page, type, zone, other) {
        $.get("/bookcity/book/list", {
            "page": page,
            "per_page": per_page,
            "type": type,
            "zone": zone,
            "other": other
        },
            write_book);
    }

    /**
     * 清除 lis 的 selected 类
     * @param {JQuery} lis 
     */
    function clear_selected(lis) {
        lis.each(function (i) {
            $(this).removeClass("selected");
        });
    }

    /**
     * 根据页码和索引计算新的页码
     * @param {Number} pn 页码
     * @param {Number} i 所选元素的索引
     * @returns 当前所选页码
     */
    function get_page_number(pn, i) {
        if (i == 0)
            pn -= 1;
        else if (i == $(".pageItems li").length - 1)
            pn += 1;
        else
            pn = i - 1;
        if (pn < 0)
            pn = 0;
        return pn;
    }

    // 搜索书籍
    $(".search button").on("click", function () {
        $.post({
            url: "/bookcity/book/search",
            data: {
                "kw": $("#keyword").val()
            },
            success: write_book
        })
    })
};